# _*_ coding: UTF-8 _*_
# @Time     : 2021/10/26 下午 03:37
# @Author   : Li Jie
# @Site     : http://www.hzdledu.cn/
# @File     : c10s1_文件操作.py
# @Software : PyCharm

def foo1():
    # 打开/创建文件
    file1 = open('./test1.txt', mode='r', encoding='utf8')
    # file2 = open(r'D:\\pyworkspacehz_t2\c10\test1.txt')

    # 关闭
    file1.close()


def foo2():
    # with-open
    print('-------------------------------')
    with open('./test1.txt', mode='r', encoding='utf8') as file:
        # 1.read():读取整个文件或指定个数的字符串
        str1 = file.read()
        print(str1)


def foo3():
    # with-open
    print('-------------------------------')
    with open('./test1.txt', mode='r', encoding='utf8') as file:
        # 2.readline():读取文件中的一行
        str1 = file.readline()
        print(str1)


def foo4():
    # with-open
    print('-------------------------------')
    with open('./test1.txt', mode='r', encoding='utf8') as file:
        # 3.readlines():读取文件中的所有行，放入一个列表
        list1 = file.readlines()
        print(list1)


# 文件写入
def foo5():
    with open('./test2.txt', 'w', encoding='utf') as file:
        str1 = '犇\n汇智动力\n2021'
        # write():将字符串内容写入文件
        file.write(str1)


def foo6():
    with open('./test3.txt', 'w', encoding='utf') as file:
        # 将字符串列表写入文件
        list1 = ['骉\n', '汇智动力\n', '2022']
        file.writelines(list1)


if __name__ == '__main__':
    # foo1()
    # foo2()
    # foo3()
    # foo4()
    foo5()
    foo6()
